package com.webserver.controller;

import com.webserver.http.HttpRequest;
import com.webserver.http.HttpResponse;
import com.webserver.vo.User;

import javax.sound.midi.VoiceStatus;
import java.io.*;

public class UserController {
    //保存所有用户信息的目录名字
    private static String userDirName = "./users/";

    static {
        //程序加载时判断一下保存所有用户信息的目录是否存在，不存在先自动创建出来
        File userDir = new File(userDirName);
        if (!userDir.exists()){
            userDir.mkdirs();
        }
    }
    /*
       用来处理用户注册操作
       @param request
       @param response
    */
    public void register(HttpRequest request,HttpResponse response ){
        System.out.println("开始处理用户注册......");
        //1.从request中获取用户表单上提交的注册信息
        /*注：request.getParameter()获取表单某输入框上的内容要传
        递的参数为该输入框name=""属性指定的值*/
        String uname = request.getParameter("uname");
        String password = request.getParameter("upwd");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(uname+","+password+","+nickname+","+ageStr);

       /*
            必要的验证工作，保证注册的四个信息不为空，否则一直响应注册失败的提示页面
            年龄还要求必须是一个数字格式
            失败页面：registerError.html,居中显示一行字：注册失败，输入信息有误，请重新注册
       */

        if (uname==null||password==null||nickname==null||ageStr==null||
                !ageStr.matches("[0-9]+")){
            response.setEntity(new File("./webapps/myweb/registerError.html"));
            return;
        }


        /*
          判断是否为重复用户，如果是重复用户则直接响应页面：registerExists.html
          该页面居中显示一行字：该用户已存在请重新注册
        */

        File userFile = new File(userDirName+uname+".obj");
        if (userFile.exists()){
            response.setEntity(new File("./webapps/myweb/registerExists.html"));
            return;
        }


        int age = Integer.parseInt(ageStr);

        //2.将该用户信息写入磁盘保存
        User user = new User(uname,password,nickname,age);
        try(
                ObjectOutputStream oos = new ObjectOutputStream(
                        //./users/zqz.obj
                        new FileOutputStream(userDirName+uname+".obj"));
                ){
            oos.writeObject(user);
            //注册成功！
            response.setEntity(new File("./webapps/myweb/registerOk.html"));
        }catch (IOException e){
            e.printStackTrace();
        }

        //3.设置response响应注册结果页面
        System.out.println("用户注册处理完毕......");
    }

    public void login(HttpRequest request,HttpResponse response){
        System.out.println("开始处理用户登录.....");
        String uname = request.getParameter("uname");
        String password = request.getParameter("upwd");
        if (uname==null||password==null){
            response.setEntity(new File("./webapps/myweb/loginError.html"));
            return;
        }

        File userFile = new File(userDirName+uname+".obj");
        if (userFile.exists()){
            try (
                ObjectInputStream ois = new ObjectInputStream(new FileInputStream(userFile)
                )
            ) {
                User user = (User)ois.readObject();
                if (user.getPassword().equals(password)){
                    response.setEntity(new File("./webapps/myweb/loginSuccess.html"));
                    return;
                }
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        response.setEntity(new File("./webapps/myweb/loginFail.html"));
        System.out.println("用户登录处理完毕.....");
    }



}













